﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PersonalHealth.Data.Model;
using PersonalHealth.Data.IService;
using PersonalHealth.Data.Service;

namespace PersonHealth.View
{
    /// <summary>
    /// MessagePage.xaml 的交互逻辑
    /// </summary>
    public partial class MessagePage : Page
    {
        int pageIndex = 1;
        int pageCount = 7;
        

        public MessagePage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MessagePage_Loaded);
        }

        void MessagePage_Loaded(object sender, RoutedEventArgs e)
        {
            // System.Threading.Thread.Sleep(1000);
            
        }

        private void allMsg_Checked(object sender, RoutedEventArgs e)
        {
            if (sysMsg != null)
                sysMsg.IsChecked = false;
            if (docMsg != null)
                docMsg.IsChecked = false;

            pageIndex = 1;
            bindData();
        }

        private void sysMsg_Checked(object sender, RoutedEventArgs e)
        {
            if (allMsg != null)
                allMsg.IsChecked = false;
            if (docMsg != null)
                docMsg.IsChecked = false;

            pageIndex = 1;
            bindData();
        }

        private void docMsg_Checked(object sender, RoutedEventArgs e)
        {
            if (allMsg != null)
                allMsg.IsChecked = false;
            if (sysMsg != null)
                sysMsg.IsChecked = false;

            pageIndex = 1;
            bindData();
        }


      

        private void Previous_Click(object sender, RoutedEventArgs e)
        {
            if (pageIndex == 1)
                return;
            pageIndex--;
            bindData();
        }

        private void Next_Click(object sender, MouseButtonEventArgs e)
        {
            if (this.DataContext!= null && pageIndex == (this.DataContext as MessageModel).Pages)
                return;
            pageIndex++;
            bindData();
        }

        private void bindData()
        {
            MeasureUser user = AppData.User;
            IMessageService ser = new MessageService();

            int msgCount = 0;
            int notReadCount = 0;
            IList<Message> objs = null;
            if (allMsg != null && allMsg.IsChecked == true)
            {
                objs = ser.GetUserMessages(user.Id, pageIndex, pageCount, out msgCount, out notReadCount);
            }
            if (sysMsg != null && sysMsg.IsChecked == true)
            {
                objs = ser.GetUserMessages(user.Id, MessageType.System, pageIndex, pageCount, out msgCount, out notReadCount);

            }
            if (docMsg != null && docMsg.IsChecked == true)
            {
                objs = ser.GetUserMessages(user.Id, MessageType.Doctor, pageIndex, pageCount, out msgCount, out notReadCount);

            }

            if (objs != null)
            {
                foreach (var i in objs)
                {
                    if (i.Msg.Length > 30)
                    {
                        i.Description = i.Msg.Substring(0, 30) + "...";
                    }
                    else
                    {
                        i.Description = i.Msg;
                    }
                }
                MessageList.ItemsSource = objs;

                MessageModel model = new MessageModel();
                model.TotalMsgs = msgCount;
                model.NotReadMsgs = string.Format("({0}条未读消息)", notReadCount);
                model.PageIndex = pageIndex;
                model.PageCount = pageCount;
                model.Pages = (msgCount + pageCount - 1) / pageCount;
                this.DataContext = model;
            }
        }

        private void Grid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (MessageList.SelectedItem != null)
            {
                var obj = (Message)MessageList.SelectedItem;
                if (obj.NotRead)
                {
                    MeasureUser user = AppData.User;
                    IMessageService ser = new MessageService();
                    ser.SetStateToHasRead(obj.Id);
                }
                NavigationService.Content = new MessageForm(obj);
            }
        }
    }

    public class MessageModel
    {
        public int TotalMsgs { get; set; }

        public string NotReadMsgs { get; set; }

        public int PageCount { get; set; }

        public int PageIndex { get; set; }

        public int Pages { get; set; }
    }

}
